package cn.soft863.orders.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.soft863.bike.pojo.ABike;
import cn.soft863.bike.service.IBikeService;
import cn.soft863.bike.service.impl.BikeServiceImpl;
import cn.soft863.common.util.PageInfoUtil;
import cn.soft863.orders.dao.IAOrdersDao;
import cn.soft863.orders.dao.IAOrdersHistoryDao;
import cn.soft863.orders.dao.impl.AOrdersDaoImpl;
import cn.soft863.orders.dao.impl.AOrdersHistoryDaoImpl;
import cn.soft863.orders.pojo.AOrders;
import cn.soft863.orders.pojo.AOrdersHistory;
import cn.soft863.orders.service.IOrdersService;
import cn.soft863.users.pojo.ABalance;
import cn.soft863.users.service.IUsersService;
import cn.soft863.users.service.impl.UsersServiceImpl;

public class OrdersServiceImpl implements IOrdersService
{

	private IAOrdersDao orderDao = new AOrdersDaoImpl();
	private IAOrdersHistoryDao orderHistoryDao = new AOrdersHistoryDaoImpl();
	
	/* 持有一个用户的Service */
	private IUsersService usersService = new UsersServiceImpl();
	private IBikeService bikeService = new BikeServiceImpl() ; 

	/**
	 * 验证一下之前是否有正在骑的车:
	是否得验证一下余额
	插入订单表一条记录
	插入订单操作历史表一条记录
	 */
	@Override
	public Map<String, Object> saveOneorderService(AOrders order)
	{
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 验证一下之前是否有正在骑的车
		 * 按照用户id和订单状态查询(10:进行中)
		 *  */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 10是进行中 */
		condMap.clear();
		condMap.put("status", "10");
		condMap.put("usersId", order.getUsersId());
		/* 查询记录,是调用查询一条记录呢?
		 * 还是调用查询多条记录呢? */
		PageInfoUtil pageInfoUtil = new PageInfoUtil() ;
		this.findCondListorderService(pageInfoUtil, condMap);
		
		/* 判断总记录数,不需要接收List */
		if(pageInfoUtil.getTotalRecord() > 0)
		{
			resultMap.put("code","1");
			resultMap.put("info","您的有订单还在进行中,不能下单");
			return resultMap ; 
		}
		
		/* 是否得验证一下余额 */
		/* 根据用户id查询和类型出余额 */
		condMap.clear();
		condMap.put("usersId", order.getUsersId());
		condMap.put("bType", "0");
		ABalance balance = this.usersService.findOneBalanceService(condMap);
		if(balance != null && balance.getBalance() <= 0 )
		{
			resultMap.put("code","2");
			resultMap.put("info","余额不足");
			return resultMap ;
		}
		
		/* 为订单的名称赋值:存储的是车辆的名称 */
		/* 根据车辆id查询车辆信息 */
		condMap.clear();
		condMap.put("id", order.getBikeId());
	
		ABike bike = this.bikeService.findOneBikeService(condMap);
		order.setName(bike.getName());
		
		/* 往订单表插入一条记录 */
		order.setStatus(Byte.valueOf("10"));
		int res =  this.orderDao.saveOneDao(order);
		if(res > 0 )
		{
			/* 插入订单成功
			 * 插入订单历史表呢?
			 * 
			 * 插入订单历史表如果抛异常了,咋办?
			 *  */
			
			AOrdersHistory ordersHistory = new AOrdersHistory() ;
			/* 订单表插入成功时,会获取主键 */
			ordersHistory.setOrdersId(order.getId());
			ordersHistory.setUsersId(order.getUsersId());
			ordersHistory.setContent("下单成功");
			ordersHistory.setStatus(order.getStatus());
			ordersHistory.setCreateTime(new Date());
			ordersHistory.setUpdateTime(new Date());
			ordersHistory.setPubTime(new Date());
			/* 插入订单历史表 */
			res = this.saveOneorderHistoryService(ordersHistory);
			if(res > 0 )
			{
				resultMap.put("code","0");
				resultMap.put("info","成功");
				resultMap.put("id",order.getId());
				resultMap.put("ordersHistoryId",ordersHistory.getId());
				return resultMap ;
			}
		}
		return resultMap ; 
	}

	@Override
	public int updateOneorderService(AOrders order)
	{
		int x = this.orderDao.updateOneDao(order);
	
	
		return x;
	}

	@Override
	public List<AOrders> findCondListorderService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		return this.orderDao.findCondListDao(pageInfoUtil, condMap);
	}

	@Override
	public AOrders findOneorderService(Map<String, Object> condMap)
	{
		return this.orderDao.findOneDao(condMap);
	}
	
	@Override
	public int saveOneorderHistoryService(AOrdersHistory orderHis)
	{
		return this.orderHistoryDao.saveOneDao(orderHis);
	}

	@Override
	public int updateOneorderHistoryService(AOrdersHistory orderHis)
	{
		return this.orderHistoryDao.updateOneDao(orderHis);
	}

	@Override
	public List<AOrdersHistory> findCondListorderHistoryService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		return this.orderHistoryDao.findCondListDao(pageInfoUtil, condMap);
	}

	@Override
	public AOrdersHistory findOneorderHistoryService(Map<String, Object> condMap)
	{
		return this.orderHistoryDao.findOneDao(condMap);
	}
}
